home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / HippoDrawSrc1.1 / Hippo.subproj / Circle.m < prev    next >
Encoding:
Text File  |  1992-04-25  |  1.4 KB  |  63 lines

  1. #import "Circle.h"
  2. #import "draw.h"
  3. #import <appkit/nextstd.h>
  4. #import <dpsclient/wraps.h>
  5. #import <math.h>
  6.  
  7. @implementation Circle : Graphic
  8.  
  9. - (float)naturalAspectRatio
  10. /*
  11.  * The natural aspect ratio of an oval is 1.0 (a circle).
  12.  */
  13. {
  14.     return 1.0;
  15. }
  16.  
  17. - draw
  18. {
  19.     if (bounds.size.width < 1.0 || bounds.size.height < 1.0) return self;
  20.  
  21.     if (!NXEqualColor([self fillColor], NX_COLORCLEAR)) {
  22.     if (!NXEqualColor([self lineColor], NX_COLORCLEAR)) PSgsave();
  23.     [self setFillColor];
  24.     PSFilledOval(bounds.origin.x, bounds.origin.y,
  25.              bounds.size.width, bounds.size.height);
  26.     if (!NXEqualColor([self lineColor], NX_COLORCLEAR)) PSgrestore();
  27.     }
  28.     if (!NXEqualColor([self lineColor], NX_COLORCLEAR)) {
  29.     [self setLineColor];
  30.     PSFramedOval(bounds.origin.x, bounds.origin.y,
  31.              bounds.size.width, bounds.size.height);
  32.     }
  33.  
  34.     return self;
  35. }
  36.  
  37. - (BOOL)hit:(const NXPoint *)p
  38. /*
  39.  * Hit only if inside the interior of the oval.
  40.  */
  41. {
  42.     NXCoord x, y;
  43.     NXPoint center;
  44.     double angle, radius, diameter;
  45.  
  46.     if ([super hit:p]) {
  47.     center.x = bounds.origin.x + bounds.size.width / 2.0;
  48.     center.y = bounds.origin.y + bounds.size.height / 2.0;
  49.     diameter = MIN(bounds.size.width, bounds.size.height);
  50.     x = fabs(center.x - p->x) / (bounds.size.width / diameter);
  51.     y = fabs(center.y - p->y) / (bounds.size.height / diameter);
  52.     angle = atan2(y, x);
  53.     radius = diameter / 2.0;
  54.     return(x < radius * cos(angle) && y < radius * sin(angle));
  55.     } else {
  56.     return NO;
  57.     }
  58. }
  59.  
  60.  
  61. @end
  62.  
  63.